﻿using DevExpress.XtraGrid.Demos.util;
using Luna小店.DTO;
using Luna小店.model;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.UC.面料库存
{
    public partial class Frm定制订单出库申请 : DevExpress.XtraEditors.XtraForm
    {
        private String Style_Id;
        private List<ContrastForUCDto> contrastForUCDtos = new List<ContrastForUCDto>();
        private orderStockOutApplylDTO orderStockOutApplylDTO = new orderStockOutApplylDTO();
        private Dictionary<String, String> dicOrderTypes = new Dictionary<string, string>();
        private Dictionary<String, String> dicOrderCustomerType = new Dictionary<string, string>();
        private List<KVforCombobox> OrderTypes = new List<KVforCombobox>();
        private List<KVforCombobox> OrderCustomerTypes = new List<KVforCombobox>();
        public Frm定制订单出库申请()
        {
            InitializeComponent();
        }
        public Frm定制订单出库申请(String orderId, String orderNo, String materialCode, List<ContrastForUCDto> contrastForUCDtos,String name,String styleid)
        {
            InitializeComponent();
            this.orderStockOutApplylDTO.orderId = orderId;
            this.orderStockOutApplylDTO.orderNo = orderNo;
            this.orderStockOutApplylDTO.materialCode = materialCode;
            this.contrastForUCDtos = contrastForUCDtos;
            this.Style_Id = styleid;
            this.comboBoxData();
            this.textBox面料.Text = name;
        }
        private void textBox单耗_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键和负号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            if (e.KeyChar == (char)('-'))
            {
                if (textBox单耗.Text != "")
                {
                    //MessageBox.Show("请输入正确的数字");
                    this.textBox单耗.Text = "";
                    e.Handled = true;
                }
            }
            //小数点只能输入一次  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            //第一位不能为小数点  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            //第一位是0，第二位必须为小数点  
            if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
            //第一位是负号，第二位不能为小数点  
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox单耗.Text = "";
                e.Handled = true;
            }
        }

        private void simpleButton确定_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox单耗.Text))
            {
                MessageBox.Show("单耗不能为空");
                return;
            }
            if (Convert.ToDouble(this.textBox单耗.Text) > 3)
            {
                MessageBox.Show("填写单耗过大");
                return;
            }
            this.orderStockOutApplylDTO.orderTypeId = this.comboBox订单类型.SelectedValue.ToString();
            this.orderStockOutApplylDTO.orderTypeName = this.dicOrderTypes[this.orderStockOutApplylDTO.orderTypeId];
            this.orderStockOutApplylDTO.orderCustomerTypeId = this.comboBox订单客户类型.SelectedValue.ToString();
            this.orderStockOutApplylDTO.orderCustomerTypeName = this.dicOrderCustomerType[this.orderStockOutApplylDTO.orderCustomerTypeId];
            this.orderStockOutApplylDTO.quantity = Convert.ToDouble(this.textBox单耗.Text.Trim());
            Boolean flag = FunctionHelper.JsonDeserialization<Boolean>(RestCall.httpGetMethod(URL.接口url + "/inventory/orderIsStockOut?orderId=" + this.orderStockOutApplylDTO.orderId));
            if (!flag)
            {
                RestCall.PostHttp(URL.接口url + "/inventory/saveOrderStockOutApply", FunctionHelper.JsonSerialization(this.orderStockOutApplylDTO));
                MessageBox.Show("申请完成");
            }
            else
            {
                MessageBox.Show("面料已出库不能再次申请");
            }
            this.Close();
        }

        private void comboBoxData()
        {
            this.dicOrderTypes = FunctionHelper.JsonDeserialization<Dictionary<String, String>>(RestCall.httpGetMethod(URL.接口url + "/barcode/getOrderTypeIndexAll"));
            this.dicOrderCustomerType = FunctionHelper.JsonDeserialization<Dictionary<String, String>>(RestCall.httpGetMethod(URL.接口url + "/barcode/getOrderCustomerTypeIndexAll"));
            foreach (KeyValuePair<String,String>kvp in this.dicOrderTypes)
            {
                this.OrderTypes.Add(new KVforCombobox(kvp));
            }
            foreach (KeyValuePair<String, String> kvp in this.dicOrderCustomerType)
            {
                this.OrderCustomerTypes.Add(new KVforCombobox(kvp));
            }
            this.comboBox订单类型.DataSource = this.OrderTypes;
            this.comboBox订单类型.DisplayMember = "Value";
            this.comboBox订单类型.ValueMember = "Key";
            this.comboBox订单类型.SelectedIndex = 0;

            this.comboBox订单客户类型.DataSource = this.OrderCustomerTypes;
            this.comboBox订单客户类型.DisplayMember = "Value";
            this.comboBox订单客户类型.ValueMember = "Key";
            this.comboBox订单客户类型.SelectedIndex = 0;
 
            this.gridControl辅料信息.DataSource = this.contrastForUCDtos;
        }
    }

    public class orderStockOutApplylDTO
    {
        public String orderTypeId { get; set; } //  订单类型id
        public String orderTypeName { get; set; }   //  订单类型名称
        public String orderCustomerTypeId { get; set; }  //  订单客户类型id
        public String orderCustomerTypeName { get; set; }  //  订单客户类型名称
        public String orderId { get; set; } //  订单id
        public String orderNo { get; set; } //  订单号
        public String materialCode { get; set; }    //  物料编码
        public Double quantity { get; set; }    //  需求数量
        public orderStockOutApplylDTO(string orderTypeId, string orderTypeName, string orderCustomerTypeId, string orderCustomerTypeName, string orderId, string orderNo, string materialCode, double quantity)
        {
            this.orderTypeId = orderTypeId;
            this.orderTypeName = orderTypeName;
            this.orderCustomerTypeId = orderCustomerTypeId;
            this.orderCustomerTypeName = orderCustomerTypeName;
            this.orderId = orderId;
            this.orderNo = orderNo;
            this.materialCode = materialCode;
            this.quantity = quantity;
        }

        public orderStockOutApplylDTO()
        {
            this.orderTypeId = "";
            this.orderTypeName = "";
            this.orderCustomerTypeId = "";
            this.orderCustomerTypeName = "";
            this.orderId = "";
            this.orderNo = "";
            this.materialCode = "";
            this.quantity = 0;
        }
    }
    /// <summary>
    /// 下拉框的数据源
    /// </summary>
    public class KVforCombobox
    {
        public String Key { get; set; }
        public String Value { get; set; }
        public KVforCombobox(KeyValuePair<String,String> kvp)
        {
            this.Key = kvp.Key;
            this.Value = kvp.Value;
        }
    }
}
